from django.http import HttpResponseRedirect
from django.urls import reverse

from media.lib.views import FileUploadFormMixin
from .lib import (
    HTMXProjectSubCreateFormView,
    HTMXProjectSubUpdateFormView,
    ProjectSubAuditDetailView,
    ProjectSubAuditFormPostAPIView,
    template_path,
    ProjectSubListView
)


from .. import forms, models

class InitiationListView(ProjectSubListView):
    template_name = template_path("initiation/list.html")


# NOTE special case
class InitiationCreateView(
    # FileUploadFormMixin,
    HTMXProjectSubCreateFormView
):
    form_class = forms.InitiationDataForm
    form_action_view_name = "project:initiation/create"
    create_view_name = "project:initiation/create"
    detail_view_name = "project:initiation/detail"

    def form_valid(self, form):
        obj = form.save(commit=False)
        project = self.get_project()
        initiationdata_number = project.initiationdata_set.count()
        obj.project = project
        obj.version = initiationdata_number
        obj.save()
        self.object = obj
        form.store_upload_files(self.object, self.request.user)
        return HttpResponseRedirect(self.get_success_url())

class InitiationDetailView(ProjectSubAuditDetailView):
    model = models.InitiationData
    form_class = forms.InitiationDataForm
    template_name = template_path("initiation/detail.html")
    audit_permission = "user.can_manage_projects"
    audit_viewname = "project:initiation/audit"

class InitiationUpdateView(
    FileUploadFormMixin,
    HTMXProjectSubUpdateFormView
):
    model = models.InitiationData
    form_class = forms.InitiationDataForm
    update_view_name = "project:initiation/update"
    detail_view_name = "project:initiation/detail"
    
class InitiationAuditFormPostAPIView(ProjectSubAuditFormPostAPIView):
    model = models.InitiationData
    

    
